﻿<!DOCTYPE html>
<html>
	<head>
		<title>微信授权登录</title>
		<meta name="format-detection" content="telephone=no,email=no" />
		<meta name="viewport" content="width=device-width, user-scalable=no">
		<script type="text/javascript" src="src/agency/assets/js/jquery.min.js"></script>
		<script type="text/javascript" src="src/agency/assets/js/layer/layer.js"></script>
		<script type="text/javascript" src="src/agency/openapiConfig.js"></script>
	</head>
	<body onload="isAuthorize()">
	</body>
	<script>
		var openApiUrl = openApiObj.apiUrl;
		//判断是否需要微信授权
		function isAuthorize(){
			var code = getQueryString("code"); //微信返回的code
			if(code == ""){
				//未获取到code时进行微信授权
				toWeiXin();
				return false;
			}else{
				var state = getQueryString("state"); //自定义入参
				//获取openid
				getOpenid(code, state);
			}
		}
		//获取当前页面url入参
		function getQueryString(name){
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
			var r = window.location.search.substr(1).match(reg); 
			if (r != null) {
				return decodeURI(r[2]);
			}
			return "";
		}
		//微信授权
		function toWeiXin(){
			//获取商户AppId等
			var url = window.location.href;
			var domain = url.substring(url.indexOf("//")+2,url.indexOf("."));
			$.ajax({
				url:openApiUrl + "/wx/getCompanyAppId.do",
				type:"post",
				data:{"URL":url},
				dataType:"json",
				success:function(data){
					var appId = data.appId;
					var redirect_uri = "http%3a%2f%2f"+domain+".agencych.qfstatic.com%2fvendor-h5%2fgetWxOpenid.html";
					window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_base&state=h5#wechat_redirect";
				}
			})
			//测试环境
			//var appId = "wx014c58d627deb0a2";
			//var redirect_uri = "http%3a%2f%2fwx.yixuninfo.com%2fvendor-h5%2fgetWxOpenid.html";
			//薇拉UAT环境
			//var appId = "wx199d22c75a44e805";
			//var redirect_uri = "http%3a%2f%2fagencych.qfstatic.com%2fvendor-h5%2fgetWxOpenid.html";
			//巧芯环境
			//var appId = "wx44bc5ac0301755ec";
			//var redirect_uri = "http%3a%2f%2fqiaoxin.agencych.qfstatic.com%2fvendor-h5%2fgetWxOpenid.html";
			//巧房UAT
			//var appId = "wxf1fa98468fc57d94"
			//var redirect_uri = "http%3a%2f%2fagencych.qfstatic.com%2fvendor-h5%2fgetWxOpenid.html";
			//appid = 微信商户号， redirect_uri = 需要微信回跳的页面， state = 自定义参数
			//window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_base&state=h5#wechat_redirect";
		}
		//获取openid
		function getOpenid(code, state){
			$.ajax({
				url: openApiUrl + "/wx/getWxOpenid.do",
			    type: "post",
			    dataType:'json', 
			    data:{
			    	"code":code,
			    	"state":state,
			    	"URL":window.location.href
			    },
			    success:function(data){
			    	if(data.result == "1"){
			    		//将微信openid存入本地浏览器
						window.localStorage.setItem("3_wxOpenid", data.openid);
						logining(data.openid)
			    	}else{
			    		layer.msg("自动登录失败");
			    	}
			    }
			});
		}
		//登录
		function logining(wxOpenid){
			$.ajax({
				url: openApiUrl + "/member/wxlogin.do",
			    type: "post",
			    dataType:'json', 
			    data:{
			    	"openid":wxOpenid
			    },
			    success:function(data){
			    	if(data.result == "1"){
			    		//将登录信息存入本地浏览器
						window.localStorage.setItem("3_userId", data.resData.id);
    		    		window.localStorage.setItem("3_userName", data.resData.accountName);
    		    		window.localStorage.setItem("3_phone", data.resData.phone);
				        window.localStorage.setItem("3_isLogin", true);
			    	}
		    		//跳转渠道王页面进行登录操作
					window.location.href = "index.html";
			    }
			});
		}
	</script>
</html>
